package be.djdb.generators;

import java.awt.FlowLayout;
import java.awt.LayoutManager;
import java.awt.event.ActionListener;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.swing.AbstractButton;
import javax.swing.BoxLayout;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.LayoutStyle;

import org.apache.fop.layoutmgr.FlowLayoutManager;

import be.djdb.Statics;
import be.djdb.UnConstruct;
import be.djdb.generators.data.SuperItems;
import be.djdb.preparts.MYField;
import be.djdb.utils.Projectsettings;
/**
* @author Lieven Roegiers
* @copyright 2007
* @from JAVA_mylibs
*/
@UnConstruct
public class MYpanel extends SuperItems{
		private Projectsettings settings;
		private JPanel a_panel;
		private String actiefpanel;
		private Map<String,JPanel> panels = new LinkedHashMap<String, JPanel>();

		 /**<%@ page contentType="text/html; charset=" %>
		 * @param String resourcebundlename 
		 * (name => name-lbl.properties)(name => name-tooltip.properties)
		 * or with locale
		 * (name => name-lbl_nl.properties)(name => name-tooltip_nl.properties)
		 * or country with locale
		 * (name => name-lbl_BE_nl.properties)(name => name-tooltip_BE_nl.properties)
		 */
		public MYpanel(String resourcebunlename) {
			super(resourcebunlename);
		}
		 /**<%@ page contentType="text/html; charset=" %>
		 * @param String resourcebundlename 
		 * (name => name-lbl.properties)(name => name-tooltip.properties)
		 * or with locale
		 * (name => name-lbl_nl.properties)(name => name-tooltip_nl.properties)
		 * or country with locale
		 * (name => name-lbl_BE_nl.properties)(name => name-tooltip_BE_nl.properties)
		 */
		public MYpanel(String resourcebunlename,Projectsettings settings) {
			super(resourcebunlename);
			this.settings = settings;
		}
		@UnConstruct(label="TODO translation by lbl")
		public boolean addpanelitem(String panel,String lbl,AbstractButton item){
			if (item!=null){
				item.setText(getlblFor(lbl));
				return additem(panel, lbl,item);
			}else{
				return false;
			}
		}
		/**<%@ page contentType="text/html; charset=" %>
		 * @param panel
		 * @param lbl
		 * @param item
		 * @param AnAction
		 * @return
		 */
		@UnConstruct(label="TODO translation by lbl")
		public boolean addpanelitem(String panel,String lbl,AbstractButton item,ActionListener AnAction){
			item.setText(getlblFor(lbl));
			item.addActionListener(AnAction);
			return addpanelitem(panel, lbl,item);
		}
		/**
		 * @param panel
		 * @param lbl
		 * @param item
		 * @return
		 */
		@UnConstruct(label="TODO translation by lbl")
		public boolean addpanelitem(String panel,String lbl,JLabel item){
			item.setText(getlblFor(lbl));
			if (Statics.FOLLOW){followlog( "additem Apanel To:=> "+ panel +" item:"+ lbl );	}
			if (panel.equals(this.actiefpanel)){
				if (Statics.FOLLOW){followlog(panel+" is exist" );	}
				item.setName(lbl);
				item.setToolTipText(getTooltipFor(lbl));
				a_panel.add(item);
				tools.add( item);
			}else{
				if (Statics.FOLLOW){followlog( "is not exist" );	}
				createApanel(panel);
				item.setName(lbl);
				item.setToolTipText(getTooltipFor(lbl));
				a_panel.add(item);
				tools.add( item);
				if (Statics.FOLLOW){followlog("add menu:"+panel+"met label:"+lbl );}
				panels.put(panel,a_panel);
			}
			return false;
		}
		/**
		 * 
		 * @param panel the panel by name
		 * @param lbl 
		 * @param item
		 * @return
		 */
		@UnConstruct(label="TODO translation by lbl")
		private boolean additem(String panel,String lbl,AbstractButton item){
			if (Statics.FOLLOW){followlog( "additem Apanel To:=> "+ panel +" item:"+ lbl );	}
			if (panel.equals(this.actiefpanel)){
				if (Statics.FOLLOW){followlog(panel+" is exist" );	}
				item.setName(lbl);
				item.setToolTipText(getTooltipFor(lbl));
				a_panel.add(item);
				tools.add( item);
			}else{
				if (Statics.FOLLOW){followlog( "is not exist" );	}
				createApanel(panel);
				item.setName(lbl);
				item.setToolTipText(getTooltipFor(lbl));
				a_panel.add(item);
				tools.add( item);
				if (Statics.FOLLOW){followlog("add panel:"+panel+"met label:"+lbl );}
				panels.put(panel,a_panel);
			}
			return false;
		}
		/**
		 * 
		 * @param panel
		 * @param item
		 */
		private void additem(String panel,JComponent item){
			if (panel.equals(this.actiefpanel)){
				a_panel.add(item);
				tools.add( item);
			}else{
				createApanel(panel);
				a_panel.add(item);
				tools.add( item);
				panels.put(panel,a_panel);
			}
		}
		private JPanel createApanel(String name){
	     	if (Statics.FOLLOW){followlog("createApanel:=>"+name );}
			a_panel = getpanel(name);
			if(a_panel != null){
				actiefpanel = name;
				return a_panel;
			}else {
				a_panel = new JPanel();
				a_panel.setName(name);
				panels.put(name, a_panel);
				actiefpanel = name;
				return a_panel;
			}
		}
		private void setpanelEnabled(String name, Boolean enabled){
	     	if (Statics.FOLLOW){followlog("enabled:=>"+name );}
			 JPanel panel = getpanel(name);
			if(a_panel != null){
				panel.setEnabled(enabled);
			}
		}
		public JPanel getpanel(String name){
			 return panels.get(name);
			//return X;
		}
		public void setPanel(String name, boolean enabled){
			a_panel = getpanel(name);
			if(a_panel != null){
				actiefpanel = name;
				a_panel.setEnabled(enabled);
			}
		}
		public boolean isPanel(String name){
			a_panel = getpanel(name);
			if(a_panel != null){
				actiefpanel = name;
				a_panel.isEnabled();
			}
			return false;
		}
		@UnConstruct(label="TODO translation by lbl")
		public void addpanelitem(String panel,  MYField item) {
				item.setText(getlblFor(item.getlbl()));
				if (Statics.FOLLOW){followlog( "additem Apanel To:=> "+ panel +" item:"+item.getlbl() );	}
				if (panel.equals(this.actiefpanel)){
					if (Statics.FOLLOW){followlog(panel+" is exist" );	}
					
					item.setToolTipText(getTooltipFor(item.getlbl()));
					a_panel.add(item);
					tools.add( item);
				}else{
					if (Statics.FOLLOW){followlog( "is not exist" );	}
					item.setToolTipText(getTooltipFor(item.getlbl()));
					a_panel.add(item);
					tools.add( item);
					if (Statics.FOLLOW){followlog("add menu:"+panel+"met label:"+item.getlbl() );}
					panels.put(panel,a_panel);
				}
		}
		public JPanel getpanel(String name,int axis){
			JPanel panel = panels.get(name);
			panel.setLayout(new BoxLayout(panel,axis));
			return panel;
		}
}
